package com.cloudbroker.bcs.platform.dav.middle.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.cloudbroker.bcs.platform.dav.common.config.ConfigValues;

public class ViewpointMsgFormatUtil {
    
    //观点说文本中图片格式化
    public static String formatContext(String context) {
        if (StringUtils.isEmpty(context)) {
            return context;
        }
        String regEx_img = "<img\\s*(.*?)[^>]*?>";
        Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        Matcher m_image = p_image.matcher(context);
        while (m_image.find()) {
            //找到img标签
            String img = m_image.group();
            String ossId = "";
            String w = "";
            String h = "";
            String dataW = "";
            String dataH = "";
            Matcher m = Pattern.compile("\\s+data-type\\s*=\\s*\"?(.*?)(>|\\s+)").matcher(img);
            if (m.find()) {
                String dataType = m.group(1);
                if (!StringUtils.isBlank(dataType) && dataType.contains("emoji")) {
                    continue;
                }
            }
            m = Pattern.compile("\\s+src\\s*=\\s*\"?(.*?)(>|\\s+)").matcher(img);
            if (m.find()) {
                //找到SRC
                String src = m.group(1);
                //找到ossid
                String ossPrefix = ConfigValues.getInterfaceOssAddr();
                int pre = src.indexOf(ossPrefix);
                if (pre > 0) {
                    src = src.substring(pre + ossPrefix.length());
                    ossId = src.substring(0, src.indexOf('/'));
                }
            }
            m = Pattern.compile("\\s+width\\s*=\\s*\"?(.*?)(>|\\s+)").matcher(img);
            if (m.find()) {
                w = m.group(1);
            }
            m = Pattern.compile("\\s+height\\s*=\\s*\"?(.*?)(>|\\s+)").matcher(img);
            if (m.find()) {
                h = m.group(1);
            }
            m = Pattern.compile("\\s+data-width\\s*=\\s*\"?(.*?)(>|\\s+)").matcher(img);
            if (m.find()) {
                dataW = m.group(1);
            }
            m = Pattern.compile("\\s+data-height\\s*=\\s*\"?(.*?)(>|\\s+)").matcher(img);
            if (m.find()) {
                dataH = m.group(1);
            }
            context = context.replace(img, "<img " + ossId + '|' + w + '|' + h + '|' + dataW + '|' + dataH + ">");
        }
        return context;
    }
    
    //观点说文本中图片解析
    public static String parseContext(String context) {
        if (StringUtils.isEmpty(context)) {
            return context;
        }
        String ossPrefix = ConfigValues.getInterfaceOssAddr();
        String regEx_img = "<img\\s*(.*?)[^>]*?>";
        Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        Matcher m_image = p_image.matcher(context);
        while (m_image.find()) {
            //找到img标签
            String img = m_image.group();
            String datastr = img.replace("<img", "").replace(">", "").trim();
            String[] datas = datastr.split("\\|");
            if (datas.length == 5) {
                String w = "".equals(datas[1]) ? "" : " width=" + datas[1] + " ";
                String h = "".equals(datas[2]) ? "" : " height=" + datas[2] + " ";
                String dw = "".equals(datas[3]) ? "" : " data-width=" + datas[3] + " ";
                String dh = "".equals(datas[4]) ? "" : " data-height=" + datas[4] + " ";
                String src = " src=\\\"" + ossPrefix + datas[0] + "/92f17e36e6cf0a8083ff435fc708a372\\\" ";
                String imgStr = "<img " + src + w + h + dw + dh
                        + " data-type=\\\"ue-img\\\" data-json=\\\"{&quot;ossid&quot;:&quot;" + datas[0]
                        + "&quot;,&quot;format&quot;:&quot;gif&quot;,&quot;size&quot;:&quot;0&quot;}\\\""
                        + " class=\\\"img-upload content-img\\\" type=\\\"img_upload\\\"/>";
                context = context.replace(img, imgStr);
            }
        }
        return context;
    }
    
    public static void main(String[] args) {
        String context = "<p><img src=\"http://10.139.108.89:21500/fileOpenapi/func_file_download/empty_jpg_0_1479720272063_d7169/92f17e36e6cf0a8083ff435fc708a372\" class=\"img-upload content-img\" data-type=\"ue-img\" data-json=\"{&quot;ossid&quot;:&quot;empty_jpg_0_1479720272063_d7169&quot;,&quot;format&quot;:&quot;jpg&quot;,&quot;size&quot;:&quot;143279&quot;}\" data-width=\"1024\" data-height=\"768\" type=\"img_upload\"/></p><p><img src=\"http://10.139.108.89:21500/fileOpenapi/func_file_download/asdfasdf/92f17e36e6cf0a8083ff435fc708a372\" class=\"img-upload content-img\" data-type=\"ue-img\" data-json=\"{&quot;ossid&quot;:&quot;asdfasdf&quot;,&quot;format&quot;:&quot;jpg&quot;,&quot;size&quot;:&quot;143279&quot;}\" data-width=\"1024\" data-height=\"768\" type=\"img_upload\"/></p>";
        System.out.println("两张图片格式化前长度:" + context.length());
        String p = formatContext(context);
        System.out.println("两张图片格式化后长度:" + p.length());
        System.out.println(p);
        p = parseContext(p);
        System.out.println("两张图片还原后长度:" + p.length());
        System.out.println(p);
    }
    
}
